/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import Entities.Klant;
import Interfaces.BoekingInterface;
import Interfaces.InloggenInterface;
import java.io.Serializable;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpSession;


/**
 *
 * @author dwana
 */


@ManagedBean
@ViewScoped
public class ControllerInloggen implements Serializable {
    
    @Inject
    private InloggenInterface inloggenInterface;
    private Klant klant = new Klant();
    private Klant sessieKlant = new Klant();          
 
    public Klant getKlant() {
        return this.klant;
    }
    
    public void setKlant (Klant klant) {
        this.klant = klant;
    }
    
    public Klant getSessieKlant () {
        
        FacesContext context = FacesContext.getCurrentInstance();
        
        if(context != null) {
            Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
                 if(sessionMap != null) {
                    sessieKlant =  (Klant) sessionMap.get("klant");
                 }

        }
        
   
        return sessieKlant;
        
    }

    public ControllerInloggen() {
    }
    
    public String uitloggen(){
    
        FacesContext context = FacesContext.getCurrentInstance();
        
        if(context != null) {
            Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
            
            if(sessionMap != null) {
               sessionMap.remove("klant");
            }
        }
        
        return "index.xhtml";
    }
        
    public String authenticate(){
        
        FacesContext context = FacesContext.getCurrentInstance();
        
        Klant klantDatabase = inloggenInterface.klantExists(klant);
  
        
        if(klantDatabase != null){  
            context.getExternalContext().getSessionMap().put("klant", klantDatabase);
            //this.setKlant(klant);
             return "index.xhtml";
        } else{
             return "registratie.xhtml";
        } 
    }
}
